package com.example.minzhengtong

import androidx.fragment.app.Fragment
import com.example.minzhengtong.base.BaseA
import com.example.minzhengtong.base.BaseFg
import com.example.minzhengtong.data.UserBean
import com.example.minzhengtong.ui.add.LoginA
import com.example.minzhengtong.utils.LogUtil
import com.example.minzhengtong.utils.SpUtil
import com.example.minzhengtong.utils.UIUtils
import com.google.gson.Gson
import okhttp3.*
import java.io.IOException
import java.lang.ref.WeakReference
import kotlin.jvm.Throws

/**
 * created by WY on 2021/6/1.
 */
class HttpRequest {

    fun failToast() {
        UIUtils.showToastSafely("网络连接失败")
    }

    public fun login(name: String, pass: String, context: com.example.minzhengtong.ui.LoginA) {
        val weakContext = WeakReference(context)
        val okHttpClient = OkHttpClient()
        val body: RequestBody = FormBody.Builder()
//            .add("token", SPUtils.getInstance(UIUtils.getContext()).getString(AppConst.User.NEW_TOKEN, ""))
            .add("name", name)
            .add("pwd", pass)
            .build()
        val request = Request.Builder()
            .url(Const.login)
            .post(body)
            .build()
        val call = okHttpClient.newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                failToast()
//                val message = Message()
//                message.what = 0
//                handler.sendMessage(message)
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                weakContext.get()?.loginSuccess(json = response.body()!!.string())
//                val message = Message()
//                message.what = 1
//                message.obj = response.body().string()
//                handler.sendMessage(message)
            }
        })
    }

     fun login(name: String, pass: String, context: LoginA) {
        val weakContext = WeakReference(context)
        val okHttpClient = OkHttpClient()
        val body: RequestBody = FormBody.Builder()
//            .add("token", SPUtils.getInstance(UIUtils.getContext()).getString(AppConst.User.NEW_TOKEN, ""))
            .add("name", name)
            .add("pwd", pass)
            .build()
        val request = Request.Builder()
            .url(Const.login)
            .post(body)
            .build()
        val call = okHttpClient.newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                failToast()
//                val message = Message()
//                message.what = 0
//                handler.sendMessage(message)
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                weakContext.get()?.loginSuccess(json = response.body()!!.string())
//                val message = Message()
//                message.what = 1
//                message.obj = response.body().string()
//                handler.sendMessage(message)
            }
        })
    }

     fun detail(context: BaseA) {
        val weakReference = WeakReference(context)
        val okHttpClient = OkHttpClient()
        val body: RequestBody = FormBody.Builder()
            .add("token", SpUtil.getInstance().getString(Const.TOKEN))
//            .add("start", binding.start.text.toString())
//            .add("end", binding.end.text.toString())
            .build()
        val request = Request.Builder()
            .url(Const.detail)
            .post(body)
            .build()
        val call = okHttpClient.newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                failToast()
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                val json = response.body()!!.string()
                LogUtil.d("获取用户信息${json}")
                val bean = Gson().fromJson(json, UserBean::class.java)
                SpUtil.getInstance().putObject(Const.USER_BEAN, bean)
                weakReference.get()?.requestSuccess(json)
            }
        })
    }

     fun detail(context: Fragment) {
        val weakReference = WeakReference(context)
        val okHttpClient = OkHttpClient()
        val body: RequestBody = FormBody.Builder()
            .add("token", SpUtil.getInstance().getString(Const.TOKEN))
//            .add("start", binding.start.text.toString())
//            .add("end", binding.end.text.toString())
            .build()
        val request = Request.Builder()
            .url(Const.detail)
            .post(body)
            .build()
        val call = okHttpClient.newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                failToast()
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                val json = response.body()!!.string()
                LogUtil.d("获取用户信息${json}")
                val bean = Gson().fromJson(json, UserBean::class.java)
                if (1 == bean.status) {
                    SpUtil.getInstance().putObject(Const.USER_BEAN, bean)
                    if (context is BaseFg) {
                        (weakReference.get() as BaseFg).requestSuccess(json)
                    }
                } else {
                    UIUtils.showToastSafely(bean.msg)
                }
            }
        })
    }

    public fun history(context: BaseA, start: String, end: String) {
        val weakContext = WeakReference(context)
        val okHttpClient = OkHttpClient()
        val body: RequestBody = FormBody.Builder()
            .add("token", SpUtil.getInstance().getString(Const.TOKEN))
            .add("start", start)
            .add("end", end)
            .build()
        val request = Request.Builder()
            .url(Const.history)
            .post(body)
            .build()
        val call = okHttpClient.newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                UIUtils.showToastSafely("网络连接失败")
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                val json = response.body()!!.string()
                LogUtil.d("获取列表数据$json")
                weakContext.get()?.requestSuccess(json)
//                val bean = Gson().fromJson(json, UserBean::class.java)
//                if (1 == bean.status){
//                    SpUtil.getInstance().putObject(Const.USER_BEAN,bean)
//                    requestSuccess(json)
//                }else {
//                    UIUtils.showToastSafely(bean.msg)
//                }
            }
        })
    }

    public fun history(context: Fragment, start: String, end: String) {
        val weakContext = WeakReference(context)
        val okHttpClient = OkHttpClient()
        val body: RequestBody = FormBody.Builder()
            .add("token", SpUtil.getInstance().getString(Const.TOKEN))
            .add("start", start)
            .add("end", end)
            .build()
        val request = Request.Builder()
            .url(Const.history)
            .post(body)
            .build()
        val call = okHttpClient.newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                UIUtils.showToastSafely("网络连接失败")
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                val json = response.body()!!.string()
                LogUtil.d("获取列表数据$json")
                if (context is BaseFg) {
                    (weakContext.get() as BaseFg).requestSuccess(json)
                }
//                val bean = Gson().fromJson(json, UserBean::class.java)
//                if (1 == bean.status){
//                    SpUtil.getInstance().putObject(Const.USER_BEAN,bean)
//                    requestSuccess(json)
//                }else {
//                    UIUtils.showToastSafely(bean.msg)
//                }
            }
        })
    }

}